package net.scratch.netty.net.codec;

import net.scratch.netty.util.PacketBuffer;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.frame.FrameDecoder;

public class JS5HandshakeDecoder extends FrameDecoder {

	@Override
	protected Object decode(ChannelHandlerContext ctx, Channel channel,
			ChannelBuffer buffer) throws Exception {
		if (buffer.readableBytes() >= 4) {
			int opcode = buffer.readUnsignedByte();
			if (opcode == 15) {
				int version = buffer.readInt();
				if (version == 508) {
					channel.write(new PacketBuffer().put((byte) 0).toPacket());
					channel.getPipeline().replace("rs2decoder", "rs2decoder",
							new UpdateServerDecoder());
				}

			}
		}
		return null;
	}

}
